<!--  -->
<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px ; width:100%">
    <el-col>
      <strong>{{ nodeData.name }}</strong>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ nodeData.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown>
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="addDepartment">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!iscompany" @click.native="updateDepartment">查看子部门</el-dropdown-item>
              <!-- elementui封装的组件 里面没有原生的click事件,所以绑定不到,要加上native转为原生 -->
              <el-dropdown-item v-if="!iscompany" @click.native="delCompany">删除子部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { deleteCompany } from '@/api/department'
export default {
  props: {
    // 父元素传递过来的每个节点的数据对象data
    nodeData: {
      type: Object,
      required: true
    },
    iscompany: {
      type: Boolean
    }
  },
  data() {
    return {

    }
  },
  methods: {
    // 删除子部门
    async delCompany() {
      await this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      // 点击哪个 就传入id调用封装的deleteCompany进行删除
      await deleteCompany(this.nodeData.id)
      // 弹窗提示
      this.$message({ type: 'success', message: '删除成功!' })
      // 删除完要重新渲染数据 ,传递一个事件给父组件 ,让父组件调用方法重新获取数据渲染
      this.$emit('delCompany')
    },
    // 新增子部门时 将点击的数据通过传递事件传递给父组件
    addDepartment() {
      this.$emit('addDepartment', this.nodeData)
    },
    // 查看子部门时 将点击的数据通过传递事件传递给父组件
    updateDepartment() {
      this.$emit('updateDepartment', this.nodeData)
    }
  }
}

</script>
<style lang='scss' scoped>
</style>
